package com.tiger.cloud.service.system;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tiger.cloud.common.jpush.JpushInterface;
import com.tiger.cloud.common.mapper.JsonMapper;
import com.tiger.cloud.common.view.SpringUtils;
import com.tiger.cloud.model.crm.User;
import com.tiger.cloud.model.rejection.Rejection;
import com.tiger.cloud.model.system.DataObject;
import com.tiger.cloud.model.system.SysScheduleJob;
import com.tiger.cloud.service.crm.IMailListService;
import com.tiger.cloud.service.crm.IRecommandListService;
import com.tiger.cloud.service.crm.IUserService;
import com.tiger.cloud.service.rejection.IRejectionService;
import com.tiger.cloud.util.DateTimeUtil;

public class TaskExcutor {

	public void run1(SysScheduleJob job) throws Exception {
		System.out.println("-----TaskExcutor------run1-----------"+DateTimeUtil.formatDateTimetoString(DateTimeUtil.getSystemDate()));
	}

	public void run2(SysScheduleJob job) throws Exception {
		System.out.println("-----TaskExcutor-----run2------------"+DateTimeUtil.formatDateTimetoString(DateTimeUtil.getSystemDate()));
	}

	public void run3(SysScheduleJob job) throws Exception {
		System.out.println("-----TaskExcutor-----run3------------"+DateTimeUtil.formatDateTimetoString(DateTimeUtil.getSystemDate()));
	}

	/**
	 * 运行手机通讯录匹配
	 */
	public void runMailListsMatch(SysScheduleJob job) throws Exception {
		System.out.println("--------------------开始启动数据匹配-------------------");
		IMailListService mailListService=SpringUtils.getBean(IMailListService.class);
		mailListService.runMailListsMatch(null);
	}
	
	/**
	 * 系统为您匹配到1个甩单业务“房产抵押，20万，3个⽉月”，请⽴立即到“接单”列列表中查看信息并迅速联 络甩单⼈人，以免错失商机。最后，祝您成功！（安⽯石派客服） 
	 */
	public void pushMsg(SysScheduleJob scheduleJob) throws Exception {
		System.err.println("-------TaskExecutor.pushMsg.start------------"+DateTimeUtil.formatDateTimetoString(new Date())+"  "+JsonMapper.nonDefaultMapper().toJson(scheduleJob));
		if(scheduleJob.getTempId()==null){
			System.out.println("------任务执行失败--------");
			return;
		}
		DataObject dataObject=JsonMapper.nonDefaultMapper().fromJson(scheduleJob.getTempId(), DataObject.class);
		Map<String, Object> data=new HashMap<String, Object>();
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("id", null);
		params.put("content",dataObject.getMsg());
		data.put("type", "1");
		data.put("data", JsonMapper.nonDefaultMapper().toJson(params));
		SpringUtils.getBean(JpushInterface.class).sendAllClientsPushMessage(dataObject.getJpushCode(),"notification", data);
		System.err.println("-------TaskExecutor.pushMsg.end------------"+DateTimeUtil.formatDateTimetoString(new Date()));
	}
	
	/**
	 * 有x个同行对您感兴趣，已添加了您，请予已接受，以便你们后续合作交流
	 */
	public void pushPeerMsg(SysScheduleJob scheduleJob) throws Exception {
		System.err.println("-------TaskExecutor.pushPeerMsg.start------------"+DateTimeUtil.formatDateTimetoString(new Date())+"  "+JsonMapper.nonDefaultMapper().toJson(scheduleJob));
		List<User> users=SpringUtils.getBean(IRecommandListService.class).notAcceptCount();
		for (User user : users) {
			String msg="有"+user.getCount()+"个同行对您感兴趣，已添加了您，请予已接受，以便你们后续合作交流";
			Map<String, Object> data=new HashMap<String, Object>();
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("id", null);
			params.put("content",msg);
			data.put("type", "1");
			data.put("data", JsonMapper.nonDefaultMapper().toJson(params));
			SpringUtils.getBean(JpushInterface.class).sendPushMessage(user.getJpushCode(),"notification", data);
		}
		System.err.println("-------TaskExecutor.pushPeerMsg.end------------"+DateTimeUtil.formatDateTimetoString(new Date()));
	}
	
	public void pushDayNotEndRejection(SysScheduleJob scheduleJob) throws Exception {
		System.err.println("-------TaskExecutor.pushDayNotEndRejection.start------------"+DateTimeUtil.formatDateTimetoString(new Date())+"  "+JsonMapper.nonDefaultMapper().toJson(scheduleJob));
		Rejection findRejection=new Rejection();
		findRejection.setPageSize(Integer.MAX_VALUE);
		List<Rejection> rejections=SpringUtils.getBean(IRejectionService.class).findDayNotEnd(findRejection);
		for (Rejection rejection : rejections) {
			String typeText="";
			if(rejection.getLoanType().equals("0")){
				typeText="车辆抵押";
			}else if(rejection.getLoanType().equals("1")){
				typeText="无抵押";
			}else if(rejection.getLoanType().equals("2")){
				typeText="零用贷";
			}else if(rejection.getLoanType().equals("3")){
				typeText="垫资";
			}else if(rejection.getLoanType().equals("4")){
				typeText="不动产抵押";
			}else if(rejection.getLoanType().equals("5")){
				typeText="其他抵押";
			}else if(rejection.getLoanType().equals("6")){
				typeText="银行中介";
			}else if(rejection.getLoanType().equals("7")){
				typeText="资方";
			}else if(rejection.getLoanType().equals("8")){
				typeText="金融律师";
			}else if(rejection.getLoanType().equals("9")){
				typeText="征信查询";
			}else if(rejection.getLoanType().equals("10")){
				typeText="催收";
			}else if(rejection.getLoanType().equals("11")){
				typeText="资产处理";
			}else if(rejection.getLoanType().equals("12")){
				typeText="其他金服";
			}else if(rejection.getLoanType().equals("13")){
				typeText="房产经纪人";
			}else if(rejection.getLoanType().equals("14")){
				typeText="车辆经纪人";
			}else if(rejection.getLoanType().equals("15")){
				typeText="保险经纪人";
			}
			String msString="您发布的甩单信息\""+typeText+","+rejection.getAmount()+"万,"+rejection.getLoanTerm()+"个月\",目前仍未完成，建议调整内容后重新发布，预祝您顺利利找到合适的接单人!";
			Map<String, Object> data=new HashMap<String, Object>();
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("id", null);
			params.put("content",msString);
			data.put("type", "1");
			data.put("data", JsonMapper.nonDefaultMapper().toJson(params));
			SpringUtils.getBean(JpushInterface.class).sendPushMessage(rejection.getJpushCode(),"notification", data);
		}
		System.err.println("-------TaskExecutor.pushDayNotEndRejection.end------------"+DateTimeUtil.formatDateTimetoString(new Date()));
	}
	/**
	 * 您还有xxx个同行未添加，请及时添加为您的人脉，以便便需要时能够立即沟通联系
	 */
	public void pushPeerMsg2(SysScheduleJob scheduleJob) throws Exception {
		System.err.println("-------TaskExecutor.pushPeerMsg.start------------"+DateTimeUtil.formatDateTimetoString(new Date())+"  "+JsonMapper.nonDefaultMapper().toJson(scheduleJob));
		List<User> users=SpringUtils.getBean(IRecommandListService.class).notAddCount();
		long userCount=SpringUtils.getBean(IUserService.class).countByEntity(new User());
		for (User user : users) {
			String msg="您还有"+(userCount-user.getCount())+"个同行未添加，请及时添加为您的人脉，以便需要时能够立即沟通联系";
			Map<String, Object> data=new HashMap<String, Object>();
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("id", null);
			params.put("content",msg);
			data.put("type", "1");
			data.put("data", JsonMapper.nonDefaultMapper().toJson(params));
			SpringUtils.getBean(JpushInterface.class).sendPushMessage(user.getJpushCode(),"notification", data);
		}
		System.err.println("-------TaskExecutor.pushPeerMsg.end------------"+DateTimeUtil.formatDateTimetoString(new Date()));
	}

}